//
//  JGMainViewController.m
//  JGExpandTableDemo2
//
//  Created by mac on 15/9/17.
//  Copyright © 2015年 Free. All rights reserved.
//

#import "JGDrupViewController.h"
#import "JGDrupTableHandler.h"
#import "JGDrupSectionViewCell.h"
#import "MJExtension.h"
#import "JGClothes.h"
#import "JGSubClothes.h"

@interface JGDrupViewController ()

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) JGDrupTableHandler *tableHandler;

@property (nonatomic, strong) NSMutableArray *contributeArray;



@end

@implementation JGDrupViewController

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
        self.navigationItem.title = @"商品列表";
        
        _tableHandler = [[JGDrupTableHandler alloc] init];
        _tableHandler.controller = self;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    //初始化tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
    _tableView.backgroundView = nil;
    _tableView.dataSource = _tableHandler;
    _tableView.delegate = _tableHandler;
    _tableView.backgroundColor = BaseViewBackgroundColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self loadData];
}

//判读是否加载数据
- (void)loadData
{
    BOOL hasZero = NO;
    for (NSDictionary *dic in self.tableHandler.contentArray) {
        if ([dic[@"subClothes"] count] == 0) {
            hasZero = YES;
            break;
        }
    }
    if (self.tableHandler.contentArray.count == 0) {
        hasZero = YES;
    }
    if (hasZero)
    {
        //请求数据
        [self p_getDrupInfo];
    }
}

/**
 *  请求/加载数据(模仿类似网络请求)
 */
- (void)p_getDrupInfo
{
    [self.tableHandler.contentArray removeAllObjects];
    
    NSArray *array = [JGClothes objectArrayWithFilename:@"Category.plist"];
    
    int i = 0;
    for (JGClothes *clothe in array) {
        NSMutableDictionary *clothesDic = nil;
        if (self.tableHandler.contentArray.count <= i) {
            clothesDic = [NSMutableDictionary dictionary];
            [clothesDic setObject:@(NO) forKeyIfNotNil:@"isOpen"];
            [self.tableHandler.contentArray addObject:clothesDic];
        } else {
             clothesDic = [self.tableHandler.contentArray objectAtIndex:i];
        }
        [clothesDic setObject:clothe.name forKeyIfNotNil:@"title"];
        [clothesDic setObject:clothe.imageName forKeyIfNotNil:@"image"];
        [clothesDic setObject:@"1" forKeyIfNotNil:@"type"];
        [clothesDic setObject:clothe.subClass forKeyIfNotNil:@"subClothes"];
        
        [self.tableHandler.contentArray replaceObjectAtIndex:i withObject:clothesDic];
        i++;
    }
    
//    NSLog(@"%@",self.tableHandler.contentArray);
    
    [self.tableView reloadData];
}

- (void)hidePicker {
}


@end
